﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using CUWo.Common;
using CUWo.Common.Entities;
using CUWo.Common.StorageHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace CUWo.App
{
    public partial class StartPage : PhoneApplicationPage
    {
        public StartPage()
        {
            InitializeComponent();
            //
            InitRom();
            AddHeaderToPivotItemInPivotMain();
            //            
            //this.UserCenterTileStoryboard();
            //string aa = Convert.ToBase64String(Encoding.UTF8.GetBytes("no=Ml9lADFVM1oyU2UHMUMzWjJB&p=7fb89992d6cf7bc302ccba8ab91dedd6"));
            this.Loaded += StartPage_Loaded;
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (this.cuActiveInfo.navigatePopup != null)
            {
                this.cuActiveInfo.navigatePopup.CloseWebViewPage();
                this.cuActiveInfo.navigatePopup = null;
                e.Cancel = true;
            }

            if (this.pivotMainTrack == this.pivotMain.SelectedItem)
            {
                if (this.cuTrackHome.gridTrackFirst.Visibility == Visibility.Collapsed && this.cuTrackHome.gridTrackSecond.Visibility == Visibility.Visible)
                {
                    this.cuTrackHome.gridTrackFirst.Visibility = Visibility.Visible;
                    this.cuTrackHome.gridTrackSecond.Visibility = Visibility.Collapsed;
                    e.Cancel = true;
                }
            }

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        private void StartPage_Loaded(object sender, RoutedEventArgs e)
        {  
        }

        private void AddHeaderToPivotItemInPivotMain()
        {
            double fontSize = 50;
            Color foreground = CUWo.Common.Helper.HexToColor("#ff000000");
            Thickness margin = new Thickness(-10, -10, 0, 10);
            this.pivotMainTrack.Header = new PivotItemHelper().CreateTextBlock("流量", fontSize, foreground, margin);
            this.pivotMainContent.Header = new PivotItemHelper().CreateTextBlock("内容", fontSize, foreground, margin);
            this.pivotMainActive.Header = new PivotItemHelper().CreateTextBlock("推荐", fontSize, foreground, margin);
            this.pivotMainUserCenter.Header = new PivotItemHelper().CreateTextBlock("我的", fontSize, foreground, margin);
        }

        void InitRom()
        {
            if (!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                RomInfo romInfo = new RomInfo()
                {
                    Mobile = string.Empty,
                    Password = string.Empty,
                    IsPush = true
                };
                IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
            }
        }




        private void pivotMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PivotItem item = this.pivotMain.SelectedItem as PivotItem;

            if (item != null)
            {
                //if (this.pivotMainContent == item)
                //{
                //    //跳转至内容栏目
                //    //this.NavigationService.Navigate(new Uri("/Views/ContentPage.xaml", UriKind.Relative));
                //    if (!App.IsLoadedNewsHome || this.cuNewsHome.llsNewsChannel.ItemsSource == null)
                //    {
                //        App.IsLoadedNewsHome = true;
                //        this.cuNewsHome.BindData();
                //    }
                //}
                //else if (this.pivotMainUserCenter == item)
                //{
                //    //跳转至我的栏目
                //    //this.NavigationService.Navigate(new Uri("/Views/UserCenterPage.xaml", UriKind.Relative));
                //}
                //else if (this.pivotMainActive == item)
                //{
                //    if (!App.IsLoadedActive || this.cuActiveInfo.fic3d.ItemsSource == null)
                //    {
                //        App.IsLoadedActive = true;
                //        this.cuActiveInfo.LoadData();
                //    }
                //}
                //else
                //{
                //    //不处理
                //    this.pivotMainContent.Visibility = System.Windows.Visibility.Visible;
                //}
            }
        }


        private void imgUserCenter_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Image img = sender as Image;
            if (img == null)
                return;
            if (string.IsNullOrWhiteSpace(img.Name))
                return;
            switch (img.Name)
            {
                case "imgUserCenterSetting": App.Navigate("/Views/UserCenterPage.xaml", string.Concat("?pivotitem=", "设置")); break;
                case "imgUserCenterEmail": App.Navigate("/Views/UserCenterPage.xaml", string.Concat("?pivotitem=", "邮箱")); break;
                case "imgUserCenterMessage": App.Navigate("/Views/UserCenterPage.xaml", string.Concat("?pivotitem=", "消息")); break;
                case "imgUserCenterMain": App.Navigate("/Views/UserCenterPage.xaml", string.Concat("?pivotitem=", "登录")); break;
                case "imgUserCenterUpdatePwd": App.Navigate("/Views/UserCenterPage.xaml", string.Concat("?pivotitem=", "密码修改")); break;
            }
            
        }


        #region 动画
        /// <summary>
        /// 我的设置图片动画
        /// </summary>
        void UserCenterTileStoryboard()
        {
            StoryboardInUserCenterSetting.Begin();
            StoryboardInUserCenterSetting.Completed += (o, args) => { StoryboardInUserCenterSetting.Stop(); StoryboardOutUserCenterSetting.Begin(); };
            StoryboardOutUserCenterSetting.Completed += (o, args) => { StoryboardOutUserCenterSetting.Stop(); StoryboardInUserCenterSetting.Begin(); };
            //
            this.StoryboardInUserCenterEmail.Begin();
            StoryboardInUserCenterEmail.Completed += (o, args) => { StoryboardInUserCenterEmail.Stop(); StoryboardOutUserCenterEmail.Begin(); };
            this.StoryboardOutUserCenterEmail.Completed += (o, args) => { StoryboardOutUserCenterEmail.Stop(); StoryboardInUserCenterEmail.Begin(); };
            //
            this.StoryboardInUserCenterMessage.Begin();
            StoryboardInUserCenterMessage.Completed += (o, args) => { StoryboardInUserCenterMessage.Stop(); StoryboardOutUserCenterMessage.Begin(); };
            this.StoryboardOutUserCenterMessage.Completed += (o, args) => { StoryboardOutUserCenterMessage.Stop(); StoryboardInUserCenterMessage.Begin(); };
            //
            this.StoryboardInUserCenterMain.Begin();
            StoryboardInUserCenterMain.Completed += (o, args) => { StoryboardInUserCenterMain.Stop(); StoryboardOutUserCenterMain.Begin(); };
            this.StoryboardOutUserCenterMain.Completed += (o, args) => { StoryboardOutUserCenterMain.Stop(); StoryboardInUserCenterMain.Begin(); };
            //
            this.StoryboardInUserCenterUpdatePwd.Begin();
            StoryboardInUserCenterUpdatePwd.Completed += (o, args) => { StoryboardInUserCenterUpdatePwd.Stop(); StoryboardOutUserCenterUpdatePwd.Begin(); };
            this.StoryboardOutUserCenterUpdatePwd.Completed += (o, args) => { StoryboardOutUserCenterUpdatePwd.Stop(); StoryboardInUserCenterUpdatePwd.Begin(); };
        }
        #endregion 动画


        private void btnAES_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //w2ol.wo.cn/online/services/ol001?channel=b94a8ac79e&param={"imei":"","appVer":"","sys":"","sysVer":"","mblNum":"18601319685","mblFirm":"","mblType":"","debug":""}
            //w2ol.wo.cn/online/services/ol001?channel=b94a8ac79e&param=1CAA356D99B9452E2F0379845C9EFDC42CDEDA0D7CD1EFA6C6C26D3ED89DBF8CBC16A58103EA3F193461B1DB9F696020C398BE83FB938BBBE91000812AC4663E827A916C2D7E7B73E06C72D8F5DC69D7C543F

            string encryptString = "{\"imei\":\"\",\"appVer\":\"\",\"sys\":\"\",\"sysVer\":\"\",\"mblNum\":\"18601319685\",\"mblFirm\":\"\",\"mblType\":\"\",\"debug\":\"\"}";
            string encryptKey = new CUWo.Common.WoInterfaceCommunication.CommunicationBase().CHANNEL_PASSWORD;
            string salt = "chinauic";
            string tmp = "AE92066915A9393C0E9C5440E06E8478352FA54C663923A9FB369C5B7C7AE75BD4277ACBAB253F7AC7C77125DE6287C11F3B026E3286219BDC209FDC09A73020329252EE80AE7ADB151BF0772AFAB67ED709961621D937A45E1A66FDD9C88CBAFBDBC9F7B7F638C09B34B98E28AA0A46";

            //EncryptAES(encryptString, encryptKey, salt);
            //var result = RunTripleDes(Encoding.UTF8.GetBytes(encryptString), hex2byte(encryptKey), true);
            //LogHelper.LogInfo(byte2hex(result));
            //LogHelper.LogInfo(Convert.ToBase64String(result));

            AESHelper.InvokeTest();

            //AESHelper aesHelper = new AESHelper(AESHelper.KeySize.Bits128, AESHelper.Hex2Byte(encryptKey));

            //var encryptByteResult = aesHelper.Cipher(Encoding.UTF8.GetBytes(encryptString));
            //var encryptStringResult = AESHelper.Byte2Hex(encryptByteResult);
            //LogHelper.LogInfo(encryptStringResult);
            //LogHelper.LogInfo("AESHelper.Cipher\t" + encryptStringResult.Equals(tmp).ToString());
            ////
            //var decryptByteResult = aesHelper.InvCipher(AESHelper.Hex2Byte(encryptStringResult));
            //var decryptStringResult = Encoding.UTF8.GetString(decryptByteResult, 0, decryptByteResult.Length);
            //LogHelper.LogInfo(decryptStringResult);
            //LogHelper.LogInfo("AESHelper.InvCipher\t" + decryptStringResult.Equals(encryptString).ToString());


        }





    }

}